<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户手机</label>
                            <div class="layui-input-inline">
                                <input type="text" name="userPhone" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">用户类型</label>
                            <div class="layui-input-block">
                                <select name="userTypeId" lay-filter="aihao">
                                    <option value="" selected=""></option>
                                    <option value="1" >新用户</option>
                                    <option value="2">老用户</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">性别</label>
                            <div class="layui-input-block">
                                <input type="radio" name="userSex" value="男" title="男">
                                <input type="radio" name="userSex" value="女" title="女">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">注册时间</label>
                            <div class="layui-input-inline">
                                <input type="text" name="userRegisterStartDate" id="start_time" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
                            </div>
                            <div class="layui-form-mid">至</div>
                            <div class="layui-input-inline">
                                <input type="text" name="userRegisterEndDate" id="end_time" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
            </div>
        </script>

        <table class="layui-hide" id="userTable" lay-filter="currentTableFilter">

        </table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="detail">详情</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>

<script>
    var detailUser;
    layui.use(['form', 'table','miniPage','element', 'laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            miniPage = layui.miniPage;
        var laydate = layui.laydate;

        /*
        日期范围
        */
        var startDate = laydate.render({
            elem: '#start_time',
            value: '1900-01-01',
            type: 'date',//设置日期的类型
            theme: '#2c78da',
            done: function (value, date) {
                if (value != "") {
                    date.month = date.month - 1;
                    endDate.config.min = date;
                } else {
                    endDate.config.min = startDate.config.min;
                }
            },
        });
        var endDate = laydate.render({
            elem: '#end_time',//绑定的控件名称
            value: '2999-12-31',
            type: 'date',//设置日期的类型
            theme: '#2c78da',
            done: function (value, date) {
                if (value != "") {
                    date.month = date.month - 1;
                    startDate.config.max = date;
                } else {
                    startDate.config.max = endDate.config.max;
                }
            }
        });

        // 执行一个table实例
        table.render({
            elem: '#userTable', // 绑定 userTable 表
            url: 'user/getUser',
            method: 'get',
            page: true, // 开启分页
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'userId', title: '用户Id', width: 10, hide: true},
                {field: 'userPhone', title: '手机号码', minWidth: 120},
                {field: 'userNickname', width: 120, title: '微信昵称'},
                {field: 'userSex', width: 80, title: '性别'},
                {field: 'userTypeName', width: 120, title: '用户类型'},
                {field: 'userRegisterDate', width: 200, title: '注册日期'},
                {field: 'orderTotalCount', width: 120, title: '订单总数'},
                {field: 'payTotal', width: 160, title: '累计消费'},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            layer.alert(result, {
                title: '搜索条件'
            });
            //执行搜索重载
            table.reload('userTable', {
                page: {
                    curr: 1
                }
                , where: data.field
                //     {
                //     // 这里有两种写法
                //     // data.field，后端 Controller 以 UserSearchParams 对象接受，但是日期无法解析
                //     // JSON.stringify(data.field)， 后端接受JSon字符串
                //     userSearchParams:
                // }
            }, 'data');
            return false;
        });


        /**
         * toolbar事件监听
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {   // 监听添加操作
                var content = miniPage.getHrefContent('user/detail.html');
                var openWH = miniPage.getOpenWidthHeight();
                var index = layer.open({
                    title: '用户详情',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('userTable')
                    , data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'detail') {
                detailUser = obj.data;
                // 详情页面该如何编写！！！！
                var content = miniPage.getHrefContent('user/detail.html');
                var openWH = miniPage.getOpenWidthHeight();
                var index = layer.open({
                    title: '用户详情',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    // 这里发送ajax请求进行删除
                    console.log(obj.data.userPhone);
                    obj.del();
                    layer.close(index);
                });
            }
        });

    });
</script>